package com.gao.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gao.weblog.common.domain.dos.StatisticsArticlePVDO;

import java.time.LocalDate;
import java.util.List;

public interface StatisticsArticlePVMapper extends BaseMapper<StatisticsArticlePVDO> {
    default int increasePVCount(LocalDate date) {
        return update(null, Wrappers.<StatisticsArticlePVDO>lambdaUpdate()
                .setSql("pv_count = pv_count + 1")
                .eq(StatisticsArticlePVDO::getPvDate, date));
    }

    default List<StatisticsArticlePVDO> selectLatestWeekRecords() {
        LambdaQueryWrapper<StatisticsArticlePVDO>  wrapper = new LambdaQueryWrapper<>();
        wrapper.le(StatisticsArticlePVDO::getPvDate, LocalDate.now().plusDays(1))
                .orderByAsc(StatisticsArticlePVDO::getPvDate)
                .last("limit 7");
        return selectList(wrapper);
    }
}
